<template>
  <div class="login-container">
    <el-form
      class="login-form"
      :rules="rules"
      ref="form"
      :model="user"
      size="medium"
      @submit.prevent="handleSubmit"
    >
      <div class="login-form__header">
        <img class="login-logo" src="@/assets/login_logo.png" alt="心选" />
      </div>
      <el-form-item prop="account">
        <el-input v-model="user.account" placeholder="请输入用户名">
          <template #prefix>
            <el-icon class="el-input__icon"><User /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input v-model="user.pwd" type="password" placeholder="请输入密码">
          <template #prefix>
            <el-icon class="el-input__icon"><lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="imgcode">
        <div class="imgcode-wrap">
          <el-input v-model="user.imgcode" placeholder="请输入验证码">
            <template #prefix>
              <el-icon class="el-input__icon"><key /></el-icon>
            </template>
          </el-input>
          <img
            class="imgcode"
            alt="验证码"
            :src="captchaSrc"
            @click="loadCaptcha"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button
          class="submit-button"
          type="primary"
          :loading="loading"
          native-type="submit"
        >
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { getCaptcha, login } from '@/api/common';
import { ElMessage } from 'element-plus';
import { store } from '@/store';
import { SET_USER } from '@/store/types';
import { useRouter, useRoute } from 'vue-router';
import type { IElForm, IFormRule } from '@/types/element-plus';

const route = useRoute();
const router = useRouter();
const form = ref<IElForm | null>(null);
const user = reactive({
  account: '',
  pwd: '',
  imgcode: '',
});
const loading = ref(false);
const rules = ref<IFormRule>({
  account: [
    {
      required: true,
      message: '请输入账号',
      trigger: 'change',
    },
  ],
  pwd: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'change',
    },
  ],
  imgcode: [
    {
      required: true,
      message: '请输入验证码',
      trigger: 'change',
    },
  ],
});
const captchaSrc = ref('');

onMounted(() => {
  loadCaptcha();
});

const loadCaptcha = async () => {
  const data = await getCaptcha();
  captchaSrc.value = URL.createObjectURL(data);
};

const handleSubmit = async () => {
  console.log('handleSubmit');
  const valid = await form.value?.validate();
  if (!valid) {
    return;
  }

  loading.value = true;
  try {
    const data = await login(user);
    if (!data) {
      return;
    }

    ElMessage.success('登录成功');

    // 存储登录用户信息
    store.commit(SET_USER, {
      ...data.user_info,
      token: data.token,
    });

    // 跳转回原来页面
    let redirect = route.query.redirect || '/';
    if (typeof redirect !== 'string') {
      redirect = '/';
    }
    router.replace(redirect);
  } catch (err) {
    loadCaptcha();
  }
  loading.value = false;
};
</script>

<style lang="scss" scoped>
.login-container {
  min-width: 400px;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;
}

.login-form {
  padding: 30px;
  border-radius: 6px;
  background: #fff;
  min-width: 350px;
  .login-form__header {
    display: flex;
    justify-content: center;
    align-items: center;
    padding-bottom: 30px;
  }

  .el-form-item:last-child {
    margin-bottom: 0;
  }

  .login__form-title {
    display: flex;
    justify-content: center;
    color: #fff;
  }

  .submit-button {
    width: 100%;
  }

  .login-logo {
    width: 271px;
    height: 74px;
  }
  .imgcode-wrap {
    display: flex;
    align-items: center;
    .imgcode {
      height: 37px;
    }
  }
}
</style>
